package com.zelin.cart.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zelin.cart.service.CartService;
import com.zelin.entity.group.Cart;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/13-14:39
 * ------------------------------
 */
@Controller
@RequestMapping("cart")
public class CartController {

    @Reference(timeout = 5000)
    public CartService cartService;

    //1. 添加商品到购物车中
    @RequestMapping("/addCart")
    public String addCart(Long itemId, int num, Model model){
        //1.0 得到登录名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println("name = " + name);
        //1.1 将指定的sku商品添加到购物车列表中
        List<Cart> cartList = cartService.addCart(name,itemId,num);
        System.out.println("cartList = " + cartList);
        //1.2 将购物车集合放到model中，送到模板中显示
        model.addAttribute("cartList",cartList);
        //1.3 返回页面
        return "cart";
    }

}
